﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BaseFunction
{
    public class EasyUIHelp
    {
        /// <summary>
        /// 把标准的datable转换成easyui所需要的json格式字符串
        /// 不能传入已分页的datable,否则总数会计算不正确
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DataTable2EasyUiDataGridJson(System.Data.DataTable dt)
        {
            System.Text.StringBuilder jsonBuilder = new System.Text.StringBuilder();
            jsonBuilder.Append("{");
            jsonBuilder.AppendFormat("\"total\":{0}, ", dt.Rows.Count);
            jsonBuilder.Append("\"rows\":[ ");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append((dt.Rows[i][j] + "").Replace("\"", ","));
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");
            jsonBuilder.Append("}");
            return jsonBuilder.ToString();
        }


        /// <summary>
        /// 配合项目中存储过程(SqlPager) 需要有3个table的DataSet,
        /// table0:空表(包含所有列名) 
        /// table1:分页信息[第1列:TotalPage总页数,2:TotalCount总记录条数,3:CurrentPage当前页码] 
        /// table2:当前分页的数据记录
        /// 转换成easyui所需要的json格式字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string PagedDataSet2EasyUiDataGridJson(System.Data.DataSet ds)
        {
            System.Text.StringBuilder jsonBuilder = new System.Text.StringBuilder();
            jsonBuilder.Append("{");
            jsonBuilder.AppendFormat("\"total\":{0}, ", ds.Tables[1].Rows[0]["TotalCount"]);
            jsonBuilder.Append("\"rows\":[ ");
            for (int i = 0; i < ds.Tables[2].Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < ds.Tables[2].Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(ds.Tables[2].Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append((ds.Tables[2].Rows[i][j] + "").Replace("\"", ","));
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");
            jsonBuilder.Append("}");
            return jsonBuilder.ToString();
        }



        /// <summary>
        /// 把树结构的datatable转换成EasyUiTreeGrid Json格式
        /// </summary>
        /// <param name="datatable">数据源</param>
        /// <param name="ID_TextField">ID列的列名(类型必须为int)</param>
        /// <param name="ParentID_TextField">父类列的列名(类型必须为int)</param>
        /// <param name="ParentID">父类ID</param>
        /// <returns></returns>
        public static string DataTable2EasyUiTreeGridJson(System.Data.DataTable datatable, string ID_TextField, string ParentID_TextField, int ParentID)
        {
            DataRow[] rows = datatable.Select(ParentID_TextField + "=" + ParentID.ToString());
            StringBuilder str = new StringBuilder();
            str.Append("[");
            foreach (DataRow row in rows)
            {
                str.Append("{");
                for (int i = 0; i < row.Table.Columns.Count; i++)
                {
                    str.Append("\"" + row.Table.Columns[i].ColumnName + "\":\"" + row[i] + "\"");
                    if (i != row.Table.Columns.Count - 1)
                    {
                        str.Append(",");
                    }
                }
                DataRow[] SubdrsTempCount = datatable.Select(ParentID_TextField + "=" + row[ID_TextField]);
                if (SubdrsTempCount.Length > 0)
                {
                    str.Append(",\"children\":");
                    str.Append(DataTable2EasyUiTreeGridJson(datatable, ID_TextField, ParentID_TextField, (int)row[ID_TextField]));
                }
                str.Append("}");
                if (!row.Equals(rows[rows.Length - 1]))
                {
                    str.Append(",");
                }
            }
            str.Append("]");
            return str.ToString();
        }

        public static string DataTable2EasyUiTreeGridJson2(DataTable datatable, string ID_TextField, string ParentID_TextField, int ParentID)
        {
             DataRow[] rows = datatable.Select(ParentID_TextField + "=" + ParentID.ToString());
            StringBuilder str = new StringBuilder();
            str.Append("[");
            foreach (DataRow row in rows)
            {
                str.Append("{");
                for (int i = 0; i < row.Table.Columns.Count; i++)
                {
                    if(row.Table.Columns[i].ToString()==ParentID_TextField) continue;
                    str.Append("\"" + row.Table.Columns[i].ColumnName + "\":\"" + row[i] + "\"");
                    if (i != row.Table.Columns.Count - 2)
                    {
                        str.Append(",");
                    }
                }
                DataRow[] SubdrsTempCount = datatable.Select(ParentID_TextField + "=" + row[ID_TextField]);
                if (SubdrsTempCount.Length > 0)
                {
                    str.Append(",\"children\":");
                    str.Append(DataTable2EasyUiTreeGridJson2(datatable, ID_TextField, ParentID_TextField, (int)row[ID_TextField]));
                }
                str.Append("}");
                if (!row.Equals(rows[rows.Length - 1]))
                {
                    str.Append(",");
                }
            }
            str.Append("]");
            return str.ToString();
        }

        /// <summary>
        /// 获取树结构json
        /// </summary>
        /// <param name="datatable">数据源</param>
        /// <param name="ID_TextField">ID列名</param>
        /// <param name="ParentID_TextField">父类列名</param>
        /// <param name="ParentID">当前需要递归的起始父类ID</param>
        /// <param name="CheckedList">checkbox被选中集合逗号隔开</param>
        /// <returns></returns>
        public static string DataTable2EasyUiTreeJson(System.Data.DataTable datatable, string ID_TextField, string ParentID_TextField, string DisplayText_TextField, int ParentID, string CheckedList)
        {
            DataRow[] rows = datatable.Select(ParentID_TextField + "=" + ParentID.ToString());
            StringBuilder str = new StringBuilder();
            str.Append("[");
            foreach (DataRow row in rows)
            {
                str.Append("{");

                str.Append("\"ID\":\"" + row[ID_TextField] + "\",\"text\":\"" + row[DisplayText_TextField] + "\",\"value\":\"" + row[ID_TextField] + "\"");
                if (CheckedList != "" && BaseFunction.StringHelp.HasContains(CheckedList, row[ID_TextField] + "", ','))
                {
                    str.Append(",\"checked\":\"indeterminate\"");
                }
                DataRow[] SubdrsTempCount = datatable.Select(ParentID_TextField + "=" + row[ID_TextField]);
                if (SubdrsTempCount.Length > 0)
                {
                    str.Append(",\"children\":");
                    str.Append(DataTable2EasyUiTreeJson(datatable, ID_TextField, ParentID_TextField, DisplayText_TextField, (int)row[ID_TextField], CheckedList));
                }
                str.Append("}");
                if (!row.Equals(rows[rows.Length - 1]))
                {
                    str.Append(",");
                }
            }
            str.Append("]");
            return str.ToString();
        }

        /// <summary>
        /// 颐阳客服数据
        /// </summary>
        /// <param name="datatable">表</param>
        /// <param name="ID_TextField">字段</param>
        /// <param name="ParentID_TextField">父节点字段</param>
        /// <param name="ParentID">父节点</param>
        /// <returns></returns>
        public static string DataTable2EasyUiUserTreeJson(System.Data.DataTable datatable, string ID_TextField, string ParentID_TextField, string DisplayText_TextField, int ParentID, string CheckedList)
        {
            DataRow[] rows = datatable.Select(ParentID_TextField + "=" + ParentID.ToString());
            StringBuilder str = new StringBuilder();
            str.Append("[");
            foreach (DataRow row in rows)
            {
                str.Append("{");

                str.Append("\"ID\":\"" + row[ID_TextField] + "\",\"text\":\"" + row[DisplayText_TextField] + "\",\"value\":\"" + row[ID_TextField] + "\"");
                if (CheckedList != "" && BaseFunction.StringHelp.HasContains(CheckedList, row[ID_TextField] + "", ','))
                {
                    str.Append(",\"checked\":\"indeterminate\"");
                }
                DataRow[] SubdrsTempCount = datatable.Select(ParentID_TextField + "=" + row[ID_TextField]);
                if (SubdrsTempCount.Length > 0)
                {
                    str.Append(",\"children\":");
                    str.Append(DataTable2EasyUiTreeJson(datatable, ID_TextField, ParentID_TextField, DisplayText_TextField, (int)row[ID_TextField], CheckedList));
                }
                str.Append("}");
                if (!row.Equals(rows[rows.Length - 1]))
                {
                    str.Append(",");
                }
            }
            str.Append("]");
            return str.ToString();
        }
    }
}
